package com.itany.controller;

import com.itany.pojo.ManagerUser;
import com.itany.service.ManagerUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Author：sl
 * Date：2019-09-17 下午6:44
 * Description：<描述>
 */
@Controller
@RequestMapping("/user")
public class ManagerUserController {

    @Autowired
    private ManagerUserService service;

    /**
     * 用户登录功能
     * @param managerUser
     */
    @RequestMapping("/login")
    public ModelAndView login(ManagerUser managerUser, HttpSession session){
        ModelAndView mv = new ModelAndView();

        try {

            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(managerUser.getUsername(),managerUser.getPassword());
            subject.login(token);


            ManagerUser s = service.login(managerUser);

            System.out.println("---------------------------------"+s);
            session.setAttribute("managerUser",s);
            mv.addObject("managerUser",s);
            mv.setViewName("/backend/index");
        } catch (Exception e){
            e.printStackTrace();
            mv.setViewName("/backend/login");
            mv.addObject("msg","服务器异常");
        }
        return mv;
    }


}
